package com.xhai.service.impl;

import com.xhai.common.domain.entity.SysUser;
import com.xhai.common.utils.classes.CollectionUtils;
import com.xhai.domain.entity.SysUserDept;
import com.xhai.domain.entity.SysUserPost;
import com.xhai.mapper.SysUserDeptMapper;
import com.xhai.mapper.SysUserMapper;
import com.xhai.mapper.SysUserPostMapper;
import com.xhai.service.SysUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author xhai
 * @since 2025-05-24
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private SysUserPostMapper sysUserPostMapper;
    @Resource
    private SysUserDeptMapper sysUserDeptMapper;

    @Override
    public SysUser selectUserByUsername(String username) {
        return sysUserMapper.selectUserByUserName(username);
    }

    @Override
    public boolean checkUserNameUnique(SysUser user) {
        // todo
        return false;
    }

    @Override
    public boolean checkPhoneUnique(SysUser user) {
        // todo
        return false;
    }

    @Override
    public boolean checkPasswordValid(SysUser user) {
        // todo
        return true;
    }

    @Override
    @Transactional
    public int insertUser(SysUser user, List<Long> deptIds, List<Long> postIds) {
        // 新增用户
        int rowNum = sysUserMapper.insert(user);

        // 新增用户部门关联
        insertUserDept(user, deptIds);

        // 新增用户岗位关联
        insertUserPost(user, postIds);

        // todo 插入后查询部门和岗位是否还存在，不存在需要回滚

        return rowNum;
    }

    /**
     * 新增用户岗位关联
     *
     * @param user    用户信息
     * @param postIds 岗位ID列表
     */
    private void insertUserPost(SysUser user, List<Long> postIds) {
        if (!CollectionUtils.isEmpty(postIds)) {
            List<SysUserPost> list = new ArrayList<>();
            for (Long postId : postIds) {
                SysUserPost sysUserPost = new SysUserPost();
                sysUserPost.setUserId(user.getUserId());
                sysUserPost.setPostId(postId);
                list.add(sysUserPost);
            }
            sysUserPostMapper.batchInsert(list);
        }
    }

    /**
     * 新增用户部门关联
     *
     * @param user    用户信息
     * @param deptIds 部门ID列表
     */
    private void insertUserDept(SysUser user, List<Long> deptIds) {
        if (!CollectionUtils.isEmpty(deptIds)) {
            List<SysUserDept> list = new ArrayList<>();
            for (Long deptId : deptIds) {
                SysUserDept sysUserDept = new SysUserDept();
                sysUserDept.setUserId(user.getUserId());
                sysUserDept.setDeptId(deptId);
                list.add(sysUserDept);
            }
            sysUserDeptMapper.batchInsert(list);
        }
    }
}
